package com.cat.dataStructure;

import java.util.HashMap;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/subarray-sums-divisible-by-k/description/
 * @create 2025/8/16 14:52
 * @since JDK17
 */

public class Solution26 {
    static HashMap<Integer, Integer> map = new HashMap<>();
    public int subarraysDivByK(int[] nums, int k) {
        map.clear();
        int n = nums.length, ans = 0, sum = 0;
        map.put(0, 1);
        for (int i = 0, key, cnt; i < n; i++) {
            sum += nums[i];
            key = (sum % k + k) % k;
            cnt = map.getOrDefault(key, 0);
            ans += cnt;
            map.put(key, cnt + 1);
        }
        return ans;
    }
}
